<?php
namespace app\common\logic;

use app\common\component\user_wallet_gold\UserWalletGoldModel;
use app\common\component\user_wallet_gold\UserWalletGoldService;

class UserWalletGold {
	public function createV01($post) {
		$post['user_id'] = \think\Session::get('user_id');
		$post['status'] = 1;
		$post['start_time'] = time();
		$post['end_time'] = $post['start_time'] + getParam('WALLET_GOLD_INTEREST_DAYS') * 24 * 60 * 60;

		return (new UserWalletGoldService()) -> createV01($post);
	}
	
	/**
	 * 获取分红
	 */
	public function getInterest() {
		$resultUserWalletGold = UserWalletGoldModel::where('status in(3, 4) AND user_id = ' . \think\Session::get('user_id')) -> find();
		if($resultUserWalletGold) {
			if($resultUserWalletGold -> status = 3) {
				// 计算剩余天数
				$remainingDays = countDays($resultUserWalletGold -> start_time);
				$WALLET_GOLD_INTEREST_DAYS = getParam('WALLET_GOLD_INTEREST_DAYS');
				if($remainingDays > $WALLET_GOLD_INTEREST_DAYS) {
					$resultUserWalletGold -> status = 4;
					$resultUserWalletGold -> interest = getParam('WALLET_GOLD_INTEREST') / 100 * $resultUserWalletGold -> price * $WALLET_GOLD_INTEREST_DAYS;

					$resultUserWalletGold -> save();
				} else {
					$interest = getParam('WALLET_GOLD_INTEREST') / 100 * $resultUserWalletGold -> price * $remainingDays;
	
					if($interest != $resultUserWalletGold -> interest) {
						$resultUserWalletGold -> interest = $interest;

						$resultUserWalletGold -> save();
					}
				}
			}

			return [
				'code' => 0,
				'data' => [
					'user_wallet_gold' => $resultUserWalletGold
				]
			];
		} else {
			return [
				'code' => 0,
				'data' => [
					'user_wallet_gold' => [
						'price' => 0,
						'interest' => 0
					]
				]
			];
		}
	}
}
?>